package cn.ym.mall.service.impl;

import cn.ym.mall.dao.ActiveJoinDao;
import cn.ym.mall.dao.impl.ActiveJoinDaoImpl;
import cn.ym.mall.entity.ActiveJoin;
import cn.ym.mall.service.ActiveJoinService;
import cn.ym.mall.utils.DruidUtils;
import cn.ym.mall.vo.Page;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 漆家乐
 */
public class ActiveJoinServiceImpl implements ActiveJoinService {
    QueryRunner queryRunner = new QueryRunner();
    ActiveJoinDao activeJoinDao = new ActiveJoinDaoImpl();
    /**
     * 新增参加活动的商家
     *
     * @param activeJoin
     * @return
     * @throws SQLException
     */
    @Override
    public boolean addActiveJoins(ActiveJoin activeJoin) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (activeJoinDao.add(activeJoin) == 1) {
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 查询所有商家（活动）
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<ActiveJoin> getAll() throws SQLException {
        Connection conn = null;
        List<ActiveJoin> activeJoinList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection();
            activeJoinList = activeJoinDao.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return activeJoinList;
    }

    /**
     * 根据id查询商家是否参加活动
     *
     * @param aid
     * @return
     * @throws SQLException
     */
    @Override
    public ActiveJoin getActiveJoinByAid(Integer aid) throws SQLException {
        Connection conn = null;
        ActiveJoin activeJoin = null;
        try {
            conn = DruidUtils.getConnection();
            activeJoin = activeJoinDao.getActiveJoinByAid(aid);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return activeJoin;
    }

    /**
     * 查询所有参加活动商家
     *
     * @param acjstate
     * @return
     * @throws SQLException
     */
    @Override
    public List<ActiveJoin> getAll(Integer acjstate) throws SQLException {
        Connection conn = null;
        List<ActiveJoin> activeJoinList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection();
            activeJoinList = activeJoinDao.getAllJoin(acjstate);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return activeJoinList;
    }

    /**
     * 分页查询商家（活动）
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public Page<ActiveJoin> getAllPage(Integer pageNo, Integer pageSize) throws SQLException {
        Connection conn = null;
        Page<ActiveJoin> page = new Page<ActiveJoin>(pageNo,pageSize);
        try {
            conn = DruidUtils.getConnection();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setCount((int)activeJoinDao.getActiveJoinsCount());
            page.setList(activeJoinDao.getAllPage(pageNo,pageSize));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }

        return page;
    }

    /**
     * 分页查询参加活动的商家
     *
     * @param acjstate
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public Page<ActiveJoin> getAllJoinPage(Integer acjstate, Integer pageNo, Integer pageSize) throws SQLException {
        Connection conn = null;
        Page<ActiveJoin> page = new Page<ActiveJoin>(pageNo,pageSize);
        try {
            conn = DruidUtils.getConnection();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setCount((int)activeJoinDao.getActiveJoinsCountByAid(acjstate));
            page.setList(activeJoinDao.getAllJoinPage(acjstate,pageNo,pageSize));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return page;
    }
}
